package com.lg.startertemplate.domain;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.lg.startertemplate.utiles.SqlUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Version;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * 数据库一定要避免大写(表名 字段名), 不然会有无尽的问题!!!
 */
@Slf4j
@Data
//@Table        //比entity 多出了很多定义,但不长用  entity必须有,没有不会建表
@Entity(name = "t_user")      //表名一定要避免大写
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE)//大写属性返回问题
@NoArgsConstructor
public class TUser extends BaseEntity {

    public TUser(String name,Integer age){
        this.name = name;
        this.age = age;
    }

    public TUser(Long sumAge,Integer maxAge){
        this.age = Math.toIntExact(sumAge);
        this.mapDefault = maxAge;
    }

    private static final long serialVersionUID = 7651803478467876293L;

    //乐观锁 保存的时候校验version
    @Version
    private Long version;


    @Min(1)
    @Max(50)
    private Integer age;

    @Column(name = "name",nullable = true,length = 50)
    private String name;

    private Boolean identify;

    //解决数据库字段 和 实体字段不一样的问题  gridset--gridSet
    @Column(name = "gridSet")       //等价于@Column(name = "grid_set")
    private String gridSet;

    /**
     * 默认的数据库不会有建立大写字段
     * 就算是指定 @Column(name = "\"WZ\"") 也会默认转成小写
     * 通过配置文件并配合@Column(name = "\"WZ\"") 可以实现让数据库 创建大写字段  但默认的驼峰规则却不识别了
     * 原文 https://www.geek-share.com/detail/2775106563.html
     * <p>
     * <p>
     * 如果vo属性为大写,在返回是会显示两条,@JsonAutoDetect 可以解决解决这个序列化的问题
     * JsonAutoDetect.Visibility.ANY : 表示所有字段都可以被发现, 包括private修饰的字段, 解决大小写问题
     * JsonAutoDetect.Visibility.NONE : 表示get方法不可见,解决字段重复问题
     * 原文  https://blog.csdn.net/cosisis/article/details/81240386
     * <p>
     * 综上数据库 一定要避免大写,所有东西都要小写!!!
     */
    @Column(name = "\"WZ\"")
    private String WZ;

    //数据库中的对应字段 map_default
    private Integer mapDefault = 0;

    public TUser(Long id, Date createTime, Date updateTime, Integer age, String name, Boolean identify) {
        super(id, createTime, updateTime);
        this.age = age;
        this.name = name;
        this.identify = identify;
    }

    public void setGridSet(String gridSet) {
        this.gridSet = "手动写的setdata优先于@data";
    }

    //自定义返回
    public static List<TUser> getTusers(List<Map<String, Object>> users) {
        ArrayList<TUser> tUsers = new ArrayList<>();
        if (users == null || users.size() == 0) {
            return tUsers;
        }
        Set<String> keys = users.get(0).keySet();

        for (Map<String, Object> user : users) {
            TUser tUser = new TUser();

            if (keys.contains("id") && user.get("id") != null)
                tUser.setId(Long.valueOf(user.get("id").toString()));

            if (keys.contains("age") && user.get("age") != null)
                tUser.setAge(Integer.valueOf(user.get("age").toString()));

            if (keys.contains("name") && user.get("name") != null)
                tUser.setName(user.get("name").toString());

            if (keys.contains("gridset") && user.get("gridset") != null)
                tUser.setGridSet(user.get("gridset").toString());

            if (keys.contains("map_default") && user.get("map_default") != null)
                tUser.setMapDefault(Integer.valueOf(user.get("map_default").toString()));

            tUsers.add(tUser);
        }
        return tUsers;
    }



    public static ArrayList<TUser> getUsers(ResultSet rs ) {
        try {
            ArrayList<TUser> list = new ArrayList<TUser>();
            HashSet<String> colName = SqlUtils.getColName(rs);
            while (rs.next()) {
                TUser obj = new TUser();
                if(colName.contains("id"))
                    obj.setId(rs.getLong("id"));
                if(colName.contains("create_time"))
                    obj.setCreateTime(rs.getDate("create_time"));;
                if(colName.contains("update_time"))
                    obj.setUpdateTime(rs.getDate("update_time"));


                if(colName.contains("name"))
                    obj.setName(rs.getString("name"));

                if(colName.contains("age"))
                   obj.setAge(rs.getInt("age"));

                list.add(obj);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }



}
